﻿Public Class Formatacao

    Public Shared Function formatCep(ByVal sValor As String) As String

        If sValor <> String.Empty Then
            If IsNumeric(sValor) Then
                sValor = sValor.ToString.Replace(".", "").Replace(",", "").Replace("/", "").Replace("-", "")
                sValor = String.Format("{0:00000\-000}", Convert.ToDouble(sValor))
            Else
                sValor = ""
            End If
        End If

        Return sValor

    End Function

    Public Shared Function formatCpf(ByVal sValor As String) As String

        If sValor <> String.Empty Then
            sValor = sValor.ToString.Replace(".", "").Replace(",", "").Replace("/", "").Replace("-", "")
            sValor = String.Format("{0:000\.000\.000\-00}", Convert.ToDouble(sValor))
        End If

        Return sValor

    End Function

    Public Shared Function formatCnpj(ByVal sValor As String) As String

        If sValor <> String.Empty Then
            sValor = sValor.ToString.Replace(".", "").Replace(",", "").Replace("/", "").Replace("-", "")
            sValor = String.Format("{0:00\.000\.000\/0000\-00}", Convert.ToDouble(sValor))
        End If

        Return sValor

    End Function

    Public Shared Function formatValor(ByVal sValor As String) As String

        If Not IsNumeric(sValor) Then
            sValor = 0
        End If
        sValor = Format(Convert.ToDouble(sValor), "#,##0.00")

        Return sValor

    End Function

    Public Shared Function formatPeso(ByVal sValor As String, ByVal iCasasDecimais As Int32) As String
        Dim sCasasDecimais As String = "0"

        sCasasDecimais = sCasasDecimais.PadRight(iCasasDecimais, "0")

        If Not IsNumeric(sValor) Then
            sValor = 0
        End If
        sValor = Format(Convert.ToDouble(sValor), "#,##0." & sCasasDecimais)

        Return sValor

    End Function

    Public Shared Function formatTelefoneComDDD(ByVal sValor As String) As String

        If sValor = String.Empty Then
            Return String.Empty
        End If
        sValor = String.Format("{0:(00)0000\-0000}", Convert.ToDouble(sValor))

        Return sValor

    End Function

    Public Shared Function formatTelefone(ByVal sValor As String) As String

        If sValor = String.Empty Then
            Return String.Empty
        End If
        sValor = String.Format("{0:0000\-0000}", Convert.ToDouble(sValor))

        Return sValor

    End Function

    Public Shared Function formatData(ByVal sValor As String) As String

        If IsDate(sValor) Then
            Return Convert.ToDateTime(sValor).ToString("dd/MM/yyyy")
        Else
            Return Date.MinValue.ToString("dd/MM/yyyy")
        End If

    End Function

End Class
